package encaps.action;

import encaps.entities.Grant;
import encaps.resources.Image;
import encaps.resources.Labels;
import encaps.resources.Response;
import encaps.utils.*;
import java.sql.SQLException;
import javax.naming.NamingException;
import org.jdom.Element;

public class ShowEmptyRolePageAction extends Action {

    public static final String SHOW_EMPTY_ROLE_PAGE = "showEmptyRolePage";
    public static final String ROLE = "role";
    public static final String NAME = "name";
    public static final String GRANT = "grant";
    public static final String GRANTS = "grants";
    protected ActionContext actionContext;

    public ShowEmptyRolePageAction(ActionContext context, Grant requiredGrant) {
        this.actionContext = context;
        this.requiredGrant = requiredGrant;
    }

    @Override
    public Element execute() throws ActionExecutionException {
        {
            try {
                Element response = XMLHelper.createElement(Response.RESPONSE);
                Element e = XMLHelper.addChild(response, Response.ENTITY);
                e.setAttribute(Response.EDIT, "true");


                XMLHelper.addChild(e, XMLHelper.createHeader(Labels.getLabel(Labels.CREATE_ROLE)));
                XMLHelper.addChild(e, XMLHelper.createTextField(Labels.getLabel(Labels.ROLE_NAME), "", NAME));
                //XMLHelper.addChild(e, XMLHelper.createTextArea(Labels.getLabel(Labels.GRANTS), grants, GRANT));


                XMLHelper.addChild(e, XMLHelper.createSmallHeader(Labels.getLabel(Labels.GRANTS)));
                XMLHelper.addChild(e, XMLHelper.createField(GRANTS, "", "", Response.MULTISELECT));
                XMLHelper.addChild(e, XMLHelper.createHiddenTextField("", GRANTS));

                XMLHelper.addChild(e, XMLHelper.createField(Labels.getLabel(Labels.MOVIE_PERSON_REMBUTTON), "f_optionDelete(['"
                        + GRANTS + "'])", "noname", Response.JSBUTTON));
                XMLHelper.addChild(e, XMLHelper.createLineBreak());
                XMLHelper.addChild(e, XMLHelper.createField(Labels.getLabel(Labels.ADD_GRANT), "f_optionMove('all','grants')", "name", Response.JSBUTTON));
                XMLHelper.addChild(e, XMLHelper.createLineBreak());
                //XMLHelper.addChild(e, XMLHelper.createLineBreak());
                try {
                    String othergrants = Provider.getUniqueString(RoleGrantGenreEnum.GRANT);
                    XMLHelper.addChild(e, XMLHelper.createField("all", othergrants, othergrants, Response.MULTISELECT));
                } catch (SQLException ex) {
                    throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
                }
                XMLHelper.addChild(e, XMLHelper.createLineBreak());
                XMLHelper.addChild(e, XMLHelper.createLineBreak());
                XMLHelper.addChild(e, XMLHelper.createImageButton(Image.getImage(Image.CREATE_ROLE), CreateRoleAction.CREATE_ROLE));

                return response;
            } catch (NamingException ex) {
                throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
            } catch (SystemException ex) {
                throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
            }
        }
    }
}
